<?PHP

// NOTE: Names in here are a little funky...

global $op_credentials;

// Called from hook wp_authenticate
function op_auth_init($username,$password){
	global $op_credentials;
	$op_credentials = array('username'=>$username,'password'=>$password); // Set credentials
	op_login();
	mysql_select_db(DB_NAME);
}

// For phpBB3
function op_login($options = array()){
	/* FIXME - This flag needs to be refined because sometimes it is from MW passing through WP.
		When logging in on phpBB, this flag needs to be set to false.
	*/
	if (!$modules = get_option('op_modules')){
		return;
	}
	
	if(isset($options['stop_WP']) && $options['stop_WP'] == TRUE && !defined('FROM_WP')){
		define('FROM_WP',TRUE);
	}
	
	if($modules['phpBB'] == 1){
		require_once(TEMPLATEPATH.'/op/modules/module.phpbb.php');
		if(file_exists(ABSPATH.'/'.PHPBBPATH.'config.php')){ // check to see if phpBB exists
			if(defined('IN_PHPBB')) return; // return if we're already in phpBB	
			
			op_phpBB3::getIdentity();
			op_phpBB3::login();
		}
	}
	
	if(file_exists(ABSPATH.'wiki/LocalSettings.php')){ // check to see if MW exists
		//require_once(TEMPLATEPATH.'/lib/modules/MediaWiki.lib.php');
		//op_MediaWiki::login();
	}
	
	return;
}

// For phpBB3
function op_logout(){
	if(!defined('FROM_WP')) define('FROM_WP',TRUE);
	
	if (!$modules = get_option('op_modules')){
		return;
	}
	
	if($modules['phpBB'] == 1){
		require_once(TEMPLATEPATH.'/op/modules/module.phpbb.php');
		if(file_exists(ABSPATH.'/'.PHPBBPATH.'config.php')){ // check to see if phpBB exists
			op_phpBB3::getIdentity();
			op_phpBB3::logout();
		}
	}
	return false;
}

function op_wp_userExists($username,$email,&$errors){
	if(op_userExists($username,'wp')){ // wordpress calls central op_userExists method
		$errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
	}
}

function op_userExists($user, $frommodule='mw'){
	// PHPBB includes
	require_once(TEMPLATEPATH.'/op/modules/module.phpbb.php');
	require_once(TEMPLATEPATH.'/op/modules/module.wordpress.php');
	//require_once(TEMPLATEPATH.'/lib/modules/MediaWiki.lib.php');
	
	$modules = array(
	//	'mw'		=>	array('class'=>'op_MediaWiki','method'=>'getUserByName'),
		'wp'		=>	array('class'=>'op_WordPress', 'method'=>'getIdByName')
	);
	
	// Get active modules and add to userExists methods
	if($active_modules = get_option('op_modules')){
		if($active_modules['phpBB'] == 1){
			$modules['phpbb'] = array('class'=>'op_phpBB3', 'method'=>'getUserByName');
		}
	}

	$modules = array_diff_key($modules,array($frommodule=>TRUE));
	
	foreach($modules as $name => $module){
		$return = call_user_func(array($module['class'],$module['method']),$user);
		if($return) break;
	}
	op_selectDbFromModule($frommodule);
	
	return $return ? TRUE : FALSE;
}

function op_updatePassword($username,$password,$frommodule='mw'){
	require_once(TEMPLATEPATH.'/op/modules/module.phpbb.php');
	require_once(TEMPLATEPATH.'/op/modules/module.wordpress.php');
	
	$modules = array();
	if($active_modules = get_option('op_modules')){
		if($active_modules['phpBB'] == 1){
			$modules['phpbb'] = array('class'=>'op_phpBB3', 'method'=>'changePassword');
		}
	}

	$modules = array_diff_key($modules,array($frommodule=>TRUE));
	
	foreach($modules as $name => $module){
		$return = call_user_func_array(array($module['class'],$module['method']),array('username'=>$username,'password'=>$password));
		if($return) break;
	}
}

function op_updateEmail($username,$email,$frommodule='mw'){
	require_once(TEMPLATEPATH.'/op/modules/module.phpbb.php');
	require_once(TEMPLATEPATH.'/op/modules/module.wordpress.php');
	
	$modules = array();
	if($active_modules = get_option('op_modules')){
		if($active_modules['phpBB'] == 1){
			$modules['phpbb'] = array('class'=>'op_phpBB3', 'method'=>'changeEmail');
		}
	}
	
	$modules = array_diff_key($modules,array($frommodule=>TRUE));
	
	foreach($modules as $name => $module){
		$return = call_user_func_array(array($module['class'],$module['method']),array('username'=>$username,'email'=>$email));
		if($return) break;
	}
}

function op_selectDbFromModule($frommodule){
	// select db from $frommodule
	switch($frommodule){
		case 'mw':
			global $wgDBname;
			mysql_select_db($wgDBname);
			break;
			
		case 'phpbb':
			global $db;
			mysql_select_db($db->dbname);
			break;
			
		case 'wp':
			mysql_select_db(DB_NAME);
			break;
	}
}

?>